package com.kedacom.ctsp.authority.util;

import com.kedacom.ctsp.authority.vo.AuthorityImportExcelResultVo;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.context.i18n.LocaleContextHolder;

import java.util.List;
import java.util.Locale;

/**
 * @批量导入数据帮助类
 * @com.kedacom.ctsp.authority.service.simple
 * @fenghaiju
 * @2018/4/9-15:44
 * @cloud-rbac
 **/
public class ImportExcelCommonUtil {

    /***
     * 返回导出结果给前端显示
     * @param serialNo
     * @param succCount
     * @param failCount
     * @param message
     * @return
     */
    public static AuthorityImportExcelResultVo setImportExcelResult(String serialNo, int succCount, int failCount, StringBuilder message) {
        AuthorityImportExcelResultVo resultVO = new AuthorityImportExcelResultVo();
        resultVO.setSerialNo(serialNo);
        Locale locale = LocaleContextHolder.getLocale();
        if (locale.getLanguage().equalsIgnoreCase("zh")) {
            resultVO.setMessage(String.format("成功%s条,失败%s条,错误消息:%s", succCount, failCount, message));
        } else {
            resultVO.setMessage(String.format("Succeeded: %s,failed: %s,error message: %s", succCount, failCount, message));
        }
        resultVO.setSuccessCount(succCount);
        resultVO.setFailCount(failCount);
        return resultVO;
    }

    /**
     * 设置上传文件日志信息
     *
     * @param message
     * @param rowNum
     * @param list
     * @return
     */
    public static void setMessage(StringBuilder message, int rowNum, List<?> list) {
        Locale locale = LocaleContextHolder.getLocale();
        if (locale.getLanguage().equalsIgnoreCase("zh")) {
            if (CollectionUtils.isNotEmpty(list)) {
                message.append(rowNum + "行：失败！");
                list.stream().forEach(e -> message.append(e + ";"));
            }
        } else {
            if (CollectionUtils.isNotEmpty(list)) {
                message.append("Row:" + rowNum + " failed!");
                list.stream().forEach(e -> message.append(e + ";"));
            }
        }
    }
}
